package com.tanhua.dubbo.api;

import com.tanhua.dubbo.utils.IdWorker;
import com.tanhua.model.mongo.Video;
import com.tanhua.model.mongo.VideoShow;
import com.tanhua.model.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

@DubboService
public class VideoApiImpl implements VideoApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private IdWorker idWorker;

    @Override
    public String save(Video video) {
        //1、设置属性
        video.setVid(idWorker.getNextId("video"));
        video.setCreated(System.currentTimeMillis());
        //2、调用方法保存对象
        mongoTemplate.save(video);
        //3、返回对象id
        return video.getId().toHexString();
    }

    // 根据id查询单个视频信息
    @Override
    public Video findVideo(String videoId) {
        Query query = Query.query(Criteria.where("id").is(videoId));
        return mongoTemplate.findOne(query, Video.class);
    }

    // 根据id查询单个视频信息
    @Override
    public Video findVideo(Long userId) {
        Query query = Query.query(Criteria.where("userId").is(userId));
        return mongoTemplate.findOne(query, Video.class);
    }

    // 查询多个视频
    @Override
    public List<Video> findVidesIds(List<Long> userIds) {
        Query query = Query.query(Criteria.where("userId").in(userIds));
        return mongoTemplate.find(query, Video.class);
    }

    // 根据粉丝人修改关注数据
    @Override
    public void updateVideo(Long userFocusId, Integer loveCount) {
        Query query = Query.query(Criteria.where("userId").is(userFocusId));
        Update update = Update.update("loveCount", loveCount);
        mongoTemplate.updateFirst(query, update, Video.class);
    }

    // 查询多条视频信息
    @Override
    public List<Video> findMovementsByVids(List<Long> vids) {
        Query query = Query.query(Criteria.where("id").in(vids));
        return mongoTemplate.find(query, Video.class);
    }

    @Override
    public List<Video> queryVideoList(int page, Integer pagesize) {
        Query query = new Query().limit(pagesize).skip((page - 1) * pagesize)
                .with(Sort.by(Sort.Order.desc("created")));
        return mongoTemplate.find(query, Video.class);
    }

    @Override
    public PageResult findByUserId(Integer page, Integer pagesize, Long userId) {
        Query query = Query.query(Criteria.where("userId").in(userId));
        long count = mongoTemplate.count(query, Video.class);
        query.limit(pagesize).skip((page - 1) * pagesize)
                .with(Sort.by(Sort.Order.desc("created")));
        List<Video> list = mongoTemplate.find(query, Video.class);
        return new PageResult(page, pagesize, count, list);
    }

    // 根据类型分页查询评论列表
    @Override
    public List<VideoShow> findVideos(String videoId, Integer comment, Integer page, Integer pagesize) {
        //1、构造查询条件
        Query query = Query.query(Criteria.where("videoId").is(new ObjectId(videoId)).and("videoType")
                .is(comment))
                .skip((page - 1) * pagesize)
                .limit(pagesize)
                .with(Sort.by(Sort.Order.desc("created")));
        //2、查询并返回
        return mongoTemplate.find(query, VideoShow.class);
    }

}
